package com.group2.bankonline.controller;

import com.group2.bankonline.pojo.Account;
import com.group2.bankonline.pojo.Mobileaccount;
import com.group2.bankonline.pojo.Recipient;
import com.group2.bankonline.pojo.Transrecord;
import com.group2.bankonline.service.*;
import com.group2.bankonline.utils.RequestAccount;
import com.group2.bankonline.utils.RequestRecipient;
import com.group2.bankonline.utils.ResponseJson;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syf
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/mobilebank/recipient")
public class RecipientController {
    @Autowired
    private IRecipientService iRecipientService;

    @Autowired
    private IMobileaccountService iMobileaccountService;

    @Autowired
    private IAccountService iAccountService;

    @Autowired
    private ITransrecordService iTransrecordService;

    @Autowired
    private IMobileService iMobileService;

    @Autowired
    private IPersoninfoService iPersoninfoService;

    /*
     * 转账操作
     * */
    @RequestMapping("/transferAccounts")
    public ResponseJson transferAccounts(@RequestBody RequestAccount requestAccount) {
        //获取表单
        String oname = requestAccount.getOthername();
        Integer oid = iAccountService.getByAccountName(oname);
        String aname = requestAccount.getAccountname();
        Integer aid = iAccountService.getByAccountName(aname);
        String password = requestAccount.getPassword();
        BigDecimal balance = requestAccount.getMoney();
        //获取支付账户
        Account aaccount = iAccountService.getById(aid);
        BigDecimal balance1 = aaccount.getBalance();
        int status = aaccount.getStatusid();
            //判断收款人和付款人是否存在
            if(aid==null){
                return new ResponseJson("400","您所输入的付款卡号不存在。");
            }else if (oid == null){
                return new ResponseJson("400","您所输入的收款卡号不存在。");
            }else{
                //判断支付账户是否冻结
                if (status == 0) {
                    return new ResponseJson("400", "您的账户被冻结，无法正常操作。");
                }
                //判断收款人与支付人是否为同一人
                if(aid==oid){
                    return new ResponseJson("400", "您不能给自己转账。");
                }
                //判断收款人对于该用户是否存在，若不存在，则添加
                Recipient recipient1 = iRecipientService.getByAIdAndOid(aid, oid);
                if(recipient1==null){
                    Recipient recipient = new Recipient();
                    recipient.setAccountid(aid);
                    recipient.setOtherid(oid);
                    //执行添加收款人操作
                    boolean result = iRecipientService.save(recipient);
                    if (!result) {
                        return new ResponseJson("400", "很抱歉，系统错误！");
                    }
                }
                //判断支付金额合理性
                if (balance.compareTo(BigDecimal.ZERO) <= 0) {
                    return new ResponseJson("400", "支付金额不可小于或者等于0");
                } else if (balance.compareTo(balance1) > 0) {
                    return new ResponseJson("400", "您所输入的转账金额超过了账户余额，很抱歉，无法进行转账操作。");
                } else {
                    //判断支付密码是否正确
                    Account account2 = iAccountService.getByIdAndPassword(aid, password);
                    if (account2 == null) {
                        return new ResponseJson("400", "您所输入的支付密码错误，请重新输入。");
                    } else {
                        //进行转账操作（支付账户减去对应金额，收款账户增加对应金额，并添加交易记录）
                        Account oaccount = iAccountService.getById(oid);
                        BigDecimal balance2 = oaccount.getBalance();
                        BigDecimal b1 = balance1.subtract(balance);
                        BigDecimal b2 = balance2.add(balance);
                        int result1 = iAccountService.updateAccountBalance(aid, b1);
                        int result2 = iAccountService.updateAccountBalance(oid, b2);
                        if (result1 > 0 && result2 > 0) {
                            Transrecord transrecord = new Transrecord();
                            transrecord.setAccountid(aid);
                            transrecord.setAccountname(aaccount.getAccountname());
                            transrecord.setOtherid(oid);
                            transrecord.setOthername(oname);
                            transrecord.setTranstypeid(2);
                            transrecord.setMoney(balance);
                            Date currentDate = new Date();
                            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                            String formatDate = format.format(currentDate);
                            transrecord.setTransdate(formatDate);
                            boolean rusult3 = iTransrecordService.save(transrecord);
                            return new ResponseJson("200", "您已转账成功！");
                        } else {
                            return new ResponseJson("400", "抱歉，转账失败！");
                        }
                    }
                }
            }

        }

}
